#include<iostream.h>
bool wanshu(int num)
{
   int sum=1,i;
   for(i=2;i<=num/2;i++)
   {
       if(num%i==0)
           sum+=i;
   }
   if(sum==num)
       return true;
   else
       return false;
}
int main()
{
   int case_num,i,j,num1,num2,temp,count;
  cin>>case_num;
   for(i=0;i<case_num;i++)
   {
       cin>>num1>>num2;
       count=0;
       if(num1>num2)
       {
           temp=num1;
           num1=num2;
           num2=temp;
       }
       if(num1%2!=0)
           num1=num1+1;
       for(j=num1;j<=num2;j=j+2)
       {
           if(wanshu(j))
           {
               count++;
           }
       }
       cout<<count<<endl;
   }
   return 0;
}